package com.example.myweather.widget;



import com.example.myweather.MyWeather;
import com.example.myweather.R;
import com.example.myweather.database.DataBaseHandler;
import com.example.myweather.database.DataBaseMapper;
import com.example.myweather.database.WidgetInitData;
import com.example.myweather.modelo.Weather;
import com.example.myweather.modelo.WeatherWebService;
import com.example.myweather.modelo.WebServicesXDotNet;
import com.example.myweather.settings.Settings;
import com.example.myweather.utilities.GetWetherImageFromText;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WeatherWidgetProvider extends AppWidgetProvider{
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		
		DataBaseMapper dataBaseMapper = DataBaseMapper.getInstance();
		
		for(int widgetId : appWidgetIds){
			WidgetInitData widgetInitData = new WidgetInitData();
			widgetInitData.setWidgetId(Integer.toString(widgetId));
			dataBaseMapper.deleteWidget(widgetInitData, context);
		}
		
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onDisabled(Context context) {
		Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
		PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.cancel(sender);
		super.onDisabled(context);
	}
	
	
	
	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
		PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
		
		//update the widget each twenty minutes (1200000 milis) 
		am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 100 * 3, 1200000 , pi);
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		ComponentName thisWidget = new ComponentName(context,
				WeatherWidgetProvider.class);
		
		DataBaseMapper dataBaseMapper = DataBaseMapper.getInstance();
				
		for (int widgetId : appWidgetManager.getAppWidgetIds(thisWidget)) {
			WidgetInitData widgetInitData = dataBaseMapper.getWidgetInitData(Integer.toString(widgetId), context);
			updateWidget(widgetInitData, context);
		}
	}
	
	public static void updateWidget(WidgetInitData widgetInitData, Context context){
		
		if(widgetInitData!=null){
			
			//Set the WebService
	        WeatherWebService webService = new WebServicesXDotNet();
	        
	        //Get Data From WebService into a Weather Object
	        Weather weather = webService.getWeather(widgetInitData.getCountryName(), widgetInitData.getCityName());
	        
	        //Check weather if weather == null an error ocurred
	        if(weather!=null){
	        	
	        	//TODO: maybe if weather is null we can return and old data saved in DataBase
	        	
		        //Get the remote views
				RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.weather_widget);
		       
				// Set the text with the current time.
				remoteViews.setTextViewText(R.id.city_name,widgetInitData.getCityName());
				remoteViews.setTextViewText(R.id.temperature, weather.getTemperature());
				remoteViews.setTextViewText(R.id.sky_conditions, weather.getSkyConditions());
				remoteViews.setTextViewText(R.id.humidity, "H"+" "+weather.getHumidity());
				remoteViews.setTextViewText(R.id.pressure, "P"+" "+weather.getPresurre());
				remoteViews.setTextViewText(R.id.max_temperature, "Max."+" "+"30°");
				remoteViews.setTextViewText(R.id.min_temperature, "Min."+" "+"10°");
				
				GetWetherImageFromText getWetherImageFromText = new GetWetherImageFromText();
				
				remoteViews.setImageViewResource(R.id.sky_condition_image, getWetherImageFromText.getImageBySkyConditions(weather.getSkyConditions()));
				
				AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
				
				//Make a Clickleable Widget
				Intent intent = new Intent(context, Settings.class);
		        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
	
		       
		        remoteViews.setOnClickPendingIntent(R.id.weather_widget_id, pendingIntent);
				
				appWidgetManager.updateAppWidget(Integer.parseInt(widgetInitData.getWidgetId()), remoteViews);
	        }
		}
	
	}
	
	

}
